﻿//-----------------------------------------------------------------------------
//<filedescription file="Program.cs" company="Microsoft">
//  <copyright>
//     Copyright © Microsoft Corporation.  All rights reserved.
//     ***  DESIGNATED SAMPLE CODE (per the included software license terms) ***
//      THIS CODE SAMPLE IS MADE AVAILABLE TO YOU WITHOUT WARRANTY OF ANY KIND
//      AND CONFERS NO RIGHTS ON YOU WHATSOEVER EXCEPT THE RIGHT TO REVIEW IT
//      SOLELY AS A REFERENCE.  THE ENTIRE RISK OF USE OR RESULTS FROM USE OF
//      THIS CODE SAMPLE REMAINS WITH YOU.
//  </copyright>
//  <purpose>
//     Sample command line front-end to the installation methods in the
//     Integration Development Kit code.
//  </purpose>
//  <notes>
//  </notes>
//</filedescription>
//-----------------------------------------------------------------------------

using System;
using System.IO;
using System.Threading;


namespace Microsoft.ConfigurationManagement.IntegrationDevelopmentKit
{
    /// <summary>
    /// 
    /// </summary>
    class InstallerCommandLineInterface
    {
        static int Main(string[] args)
        {
            try
            {

                if (string.IsNullOrEmpty(args[0]))
                {
                    Console.WriteLine(Properties.Resources.UsageInfo);
                    return 1;
                }

                string xmlFile = args[0];
                if (File.Exists(xmlFile) == false)
                {
                    Console.WriteLine(Properties.Resources.XmlFileNotFound);
                    return 1;
                }

                //string arg2 = null;
                string log = string.Empty;
                int retVal = 0;
                SccmObjectInstaller installer = new SccmObjectInstaller();

                string logDir = Directory.GetCurrentDirectory();

                if ((args.Length == 2) && (string.Compare(args[1], "asynch", StringComparison.OrdinalIgnoreCase) == 0))
                {
                    //string statusMessage = "";   
                    
                    
                    retVal = installer.InstallComponentsFromXmlFile(xmlFile, logDir, out log);

                    //while (installer.Status.StatusMessage != "Complete")
                    //{
                    //    if (installer.Status.StatusValue != SccmObjectInstaller.InstallerReturnValue.Success)
                    //    {
                    //        return 1;
                    //    }
                    //    Thread.Sleep(1000);
                    //    Console.WriteLine("\nSection   : " + installer.Status.CurrentSection);
                    //    Console.WriteLine("Status    : " + installer.Status.StatusMessage);
                    //    Console.WriteLine("% Complete: " + installer.Status.PercentComplete + "%\n\n");
                    //}
                }
                else
                {
                    Console.WriteLine("Passing the XML to InstallComponentsFromXmlFile");
                    Console.WriteLine("xmlFile: " + xmlFile);
                    Console.WriteLine("logDir: " + logDir);
                    retVal = installer.InstallComponentsFromXmlFile(xmlFile, logDir, out log);
                    Console.WriteLine("InstallComponentsFromXmlFile returned a value of " + retVal);
                }

                Console.WriteLine(log);

                if (retVal != 0)
                {
                    Console.WriteLine(Properties.Resources.RemoteProcessExitedWithError);
                    return retVal;
                }
                return 0;

            }
            catch (Exception ex)
            {
                Console.WriteLine("InstallComponentsFromXmlFile returned an exception");
                string exceptionMessage = string.Format("\n\n" +
                  "\n##############################################################################" +
                  "\n##############################################################################" +
                  "\n\nException encountered:" +
                  "\n\nException Type: " + ex.GetType().Name +
                  "\n\nMessage: " + ex.Message +
                  "\n\nTargetSite: " + ex.TargetSite + 
                  "\n\nSource:\n" + ex.Source +
                  "\n\nInnerException:\n" + ex.InnerException +
                  "\n\nStackTrace\n" + ex.StackTrace +
                  "\n\nData\n" + ex.Data +
                  "\n##############################################################################" +
                  "\n##############################################################################\n\n"
                  );

                Console.WriteLine(exceptionMessage);
                return 1;
            }
        }
    }
}
